package br.com.localfloripa.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.springframework.security.core.GrantedAuthority;

@Entity
@Table(name="perfil")
public class Perfil implements Serializable, GrantedAuthority {
	
	private static final long serialVersionUID = 8407597604443384099L;
	
	@SequenceGenerator(name="Perfil_Seq",sequenceName="perfil_seq")
	@Id @GeneratedValue(generator="Perfil_Seq")
	private Long id;	
	@Column(name="descricao",nullable=true)
	private String descricao;
	
	@ManyToMany(fetch=FetchType.LAZY)
	@JoinTable(name="usuario_perfil", joinColumns={@JoinColumn(name="perfil_id")},inverseJoinColumns={@JoinColumn(name="usuario_id")})
	private List<Usuario> usuarios = new ArrayList<Usuario>();	
	
	public Perfil() {
		
	}
	
	public Perfil(Long id) {
		this.id = id;
	}

	@Transient
	@Override
	public String getAuthority() {
		return this.descricao;
	}
	
	@Transient
	public int compareTo(Object o) {
		return this.compareTo(o);
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getDescricao() {
		return descricao;
	}

	public void setDescricao(String descricao) {
		this.descricao = descricao;
	}

	public List<Usuario> getUsuarios() {
		return usuarios;
	}

	public void setUsuarios(List<Usuario> usuarios) {
		this.usuarios = usuarios;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Perfil other = (Perfil) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

}
